/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Niveles.java
 *
 * Created on 10/11/2009, 10:04:08 PM
 */

package solucionessgh.NivelUsuario;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import solucionessgh.Util.Reporte;

/**
 *
 * @author roderick
 */
public class NivelUsuarioView extends javax.swing.JDialog {

    private NivelUsuarioModel model;
    private int id;
    private int action;//1=nuevo,2=modificar o eliminar

    public NivelUsuarioView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        model = new NivelUsuarioModel();
        setResizable(false);
        txt_nusdesc.setEditable(false);
        txt_nusabre.setEditable(false);

        btn_nuevo.setEnabled(true);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);

        java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
        java.awt.Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txt_nusdesc = new javax.swing.JTextField();
        txt_nusabre = new javax.swing.JTextField();
        btn_buscar = new javax.swing.JButton();
        btn_print = new javax.swing.JButton();
        btn_nuevo = new javax.swing.JButton();
        btn_editar = new javax.swing.JButton();
        btn_grabar = new javax.swing.JButton();
        btn_eliminar = new javax.swing.JButton();
        btn_cancelar = new javax.swing.JButton();
        btn_salir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nivel de Usuario");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(" Datos Nivel Usuario"));

        jLabel2.setText("Descripcion  :");

        jLabel3.setText("Abreviatura :");

        txt_nusdesc.setToolTipText("Ingrese descripcion");
        txt_nusdesc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_nusdescKeyTyped(evt);
            }
        });

        txt_nusabre.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                txt_nusabrePropertyChange(evt);
            }
        });
        txt_nusabre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_nusabreKeyTyped(evt);
            }
        });

        btn_buscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/find.png"))); // NOI18N
        btn_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarActionPerformed(evt);
            }
        });

        btn_print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/print.png"))); // NOI18N
        btn_print.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_printActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btn_buscar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btn_print))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_nusabre, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)
                    .addComponent(txt_nusdesc, javax.swing.GroupLayout.DEFAULT_SIZE, 211, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_print)
                    .addComponent(btn_buscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_nusdesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txt_nusabre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btn_nuevo.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_nuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/new.png"))); // NOI18N
        btn_nuevo.setText("Nuevo");
        btn_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_nuevoActionPerformed(evt);
            }
        });

        btn_editar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_editar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/edit.png"))); // NOI18N
        btn_editar.setText("Editar");
        btn_editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_editarActionPerformed(evt);
            }
        });

        btn_grabar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_grabar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/save.png"))); // NOI18N
        btn_grabar.setText("Grabar");
        btn_grabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_grabarActionPerformed(evt);
            }
        });

        btn_eliminar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_eliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/delete.png"))); // NOI18N
        btn_eliminar.setText("Eliminar");

        btn_cancelar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/close.png"))); // NOI18N
        btn_cancelar.setText("Cancelar");
        btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarActionPerformed(evt);
            }
        });

        btn_salir.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_salir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/exit.png"))); // NOI18N
        btn_salir.setText("Salir");
        btn_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_salirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btn_salir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_cancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_eliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_grabar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_editar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_nuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_nuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_editar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_grabar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_eliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_cancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_salir))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txt_nusdescKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_nusdescKeyTyped
        String abr = txt_nusdesc.getText();
        if(abr.length()>19){
            this.txt_nusdesc.setText(abr.substring(0, 19));
        }
}//GEN-LAST:event_txt_nusdescKeyTyped

    private void txt_nusabrePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_txt_nusabrePropertyChange

    }//GEN-LAST:event_txt_nusabrePropertyChange

    private void txt_nusabreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_nusabreKeyTyped
        String abr = txt_nusabre.getText();
        if(abr.length()>2){
            this.txt_nusabre.setText(abr.substring(0, 2));
        }
}//GEN-LAST:event_txt_nusabreKeyTyped

    private void btn_printActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_printActionPerformed
        Reporte rep = new Reporte("/solucionessgh/NivelUsuario/ReporteNivelUsuario.jasper");
        try {
            rep.setVisible();
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.toString());
        }
}//GEN-LAST:event_btn_printActionPerformed

    private void btn_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarActionPerformed
        BuscarView dialog = new BuscarView(new javax.swing.JFrame(), true, model);
        dialog.setVisible(true);
        if(dialog.getId()==0)
            return;

        ResultSet rs = this.model.finder(dialog.getId());
        try {
            if(rs.next()){
                id = rs.getInt("nuscod");
                txt_nusdesc.setText(rs.getString("nusdesc"));
                txt_nusabre.setText(rs.getString("nusabre"));
            }
        }catch (SQLException ex) {
            System.err.println(ex.toString());
        }

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(true);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);
}//GEN-LAST:event_btn_buscarActionPerformed

    private void btn_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_nuevoActionPerformed
        txt_nusdesc.setEditable(true);
        txt_nusabre.setEditable(true);
        txt_nusdesc.requestFocusInWindow();

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(true);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(true);

        id=0;
        action=1;
}//GEN-LAST:event_btn_nuevoActionPerformed

    private void btn_editarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_editarActionPerformed
        action=2;

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(true);
        btn_eliminar.setEnabled(true);
        btn_cancelar.setEnabled(true);

        txt_nusdesc.setEditable(true);
        txt_nusabre.setEditable(true);
        txt_nusdesc.requestFocusInWindow();
}//GEN-LAST:event_btn_editarActionPerformed

    private void btn_grabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_grabarActionPerformed
        model.setNusdesc(txt_nusdesc.getText());
        model.setNusabre(txt_nusabre.getText());
        model.setId(id);

        if(this.action==1){//grabar
            if(model.create()){
                JOptionPane.showMessageDialog(null,
                        "Grabacion OK",
                        "Moneda",
                        JOptionPane.INFORMATION_MESSAGE);
                txt_nusdesc.setText("");
                txt_nusabre.setText("");

                btn_nuevo.setEnabled(true);
                btn_editar.setEnabled(false);
                btn_grabar.setEnabled(false);
                btn_eliminar.setEnabled(false);
                btn_cancelar.setEnabled(false);
                return;
            }
        }
        if(this.action==2){//modificar
            int n = JOptionPane.showConfirmDialog(
                    null,
                    "Desea Mofiicar ",
                    "Modificar",
                    JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                if(model.update()){
                    if (n == JOptionPane.YES_OPTION) {
                        JOptionPane.showMessageDialog(null,
                                "Modificacion OK",
                                "Moneda",
                                JOptionPane.INFORMATION_MESSAGE);
                        txt_nusdesc.setText("");
                        txt_nusabre.setText("");

                        btn_nuevo.setEnabled(true);
                        btn_editar.setEnabled(false);
                        btn_grabar.setEnabled(false);
                        btn_eliminar.setEnabled(false);
                        btn_cancelar.setEnabled(false);
                        return;
                    }
                }
            }
        }
}//GEN-LAST:event_btn_grabarActionPerformed

    private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarActionPerformed
        txt_nusdesc.setText("");
        txt_nusabre.setText("");

        btn_nuevo.setEnabled(true);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);
}//GEN-LAST:event_btn_cancelarActionPerformed

    private void btn_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_salirActionPerformed
        setVisible(true);
        dispose();
}//GEN-LAST:event_btn_salirActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_buscar;
    private javax.swing.JButton btn_cancelar;
    private javax.swing.JButton btn_editar;
    private javax.swing.JButton btn_eliminar;
    private javax.swing.JButton btn_grabar;
    private javax.swing.JButton btn_nuevo;
    private javax.swing.JButton btn_print;
    private javax.swing.JButton btn_salir;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txt_nusabre;
    private javax.swing.JTextField txt_nusdesc;
    // End of variables declaration//GEN-END:variables

}
